package com.example.controller;

import com.example.common.RequirePermission;
import com.example.common.Result;
import com.example.dto.WorkScheduleDTO;
import com.example.service.WorkScheduleService;
import com.example.vo.WorkScheduleVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 排班控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/work-schedule")
@RequiredArgsConstructor
public class WorkScheduleController {
    
    private final WorkScheduleService workScheduleService;
    
    /**
     * 添加排班
     */
    @PostMapping("/add")
    @RequirePermission("store:schedule:add")
    public Result<Void> addWorkSchedule(@RequestBody WorkScheduleDTO workScheduleDTO) {
        return workScheduleService.addWorkSchedule(workScheduleDTO);
    }

    /**
     * 更新排班
     */
    @PutMapping("/update/{id}")
    @RequirePermission("store:schedule:update")
    public Result<Void> updateWorkSchedule(@PathVariable Long id, @RequestBody WorkScheduleDTO workScheduleDTO) {
        return workScheduleService.updateWorkSchedule(id, workScheduleDTO);
    }

    /**
     * 删除排班
     */
    @DeleteMapping("/delete/{id}")
    @RequirePermission("store:schedule:delete")
    public Result<Void> deleteWorkSchedule(@PathVariable Long id) {
        return workScheduleService.deleteWorkSchedule(id);
    }

    /**
     * 根据ID查询排班详情
     */
    @GetMapping("/{id}")
    @RequirePermission("store:schedule:view")
    public Result<WorkScheduleVO> getWorkScheduleById(@PathVariable Long id) {
        return workScheduleService.getWorkScheduleById(id);
    }
    
    /**
     * 获取我的排班列表
     */
    @GetMapping("/my-schedules")
    @RequirePermission("store:schedule:view")
    public Result<List<WorkScheduleVO>> getMyWorkSchedules() {
        return workScheduleService.getMyWorkSchedules();
    }

    /**
     * 根据星期几获取我的排班列表
     */
    @GetMapping("/my-schedules/day/{dayOfWeek}")
    @RequirePermission("store:schedule:view")
    public Result<List<WorkScheduleVO>> getMyWorkSchedulesByDay(@PathVariable Integer dayOfWeek) {
        return workScheduleService.getMyWorkSchedulesByDay(dayOfWeek);
    }

    /**
     * 获取我的周排班表
     */
    @GetMapping("/my-weekly-schedule")
    @RequirePermission("store:schedule:view")
    public Result<List<List<WorkScheduleVO>>> getMyWeeklySchedule() {
        return workScheduleService.getMyWeeklySchedule();
    }

    /**
     * 批量设置排班
     */
    @PostMapping("/batch-set")
    @RequirePermission("store:schedule:add")
    public Result<Void> batchSetWorkSchedules(@RequestBody List<WorkScheduleDTO> workScheduleDTOs) {
        return workScheduleService.batchSetWorkSchedules(workScheduleDTOs);
    }

    /**
     * 复制排班到其他天
     */
    @PostMapping("/copy-schedule")
    @RequirePermission("store:schedule:update")
    public Result<Void> copyScheduleToOtherDays(@RequestBody Map<String, Object> request) {
        Integer fromDay = (Integer) request.get("fromDay");
        @SuppressWarnings("unchecked")
        List<Integer> toDays = (List<Integer>) request.get("toDays");
        return workScheduleService.copyScheduleToOtherDays(fromDay, toDays);
    }

    /**
     * 清空指定星期几的排班
     */
    @DeleteMapping("/clear-day/{dayOfWeek}")
    @RequirePermission("store:schedule:delete")
    public Result<Void> clearScheduleByDay(@PathVariable Integer dayOfWeek) {
        return workScheduleService.clearScheduleByDay(dayOfWeek);
    }
    
    /**
     * 获取指定星期几的已排班时间段
     */
    @GetMapping("/occupied-time-slots/{dayOfWeek}")
    @RequirePermission("store:schedule:view")
    public Result<List<Integer[]>> getOccupiedTimeSlots(@PathVariable Integer dayOfWeek) {
        return workScheduleService.getOccupiedTimeSlots(dayOfWeek);
    }
} 